<template>
  <div class="container">
    <!-- 容器 -->
    <div class="login_container">
      <!-- logo区域 -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!--登录注册部分 -->
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-unlock"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" v-on:click="login">登录</el-button>
          <el-button type="info" v-on:click="restForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    restForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async boo => {
        if (!boo) {
          return this.$message.error('请输入正确的用户名和密码')
        }
        const { data: res } = await this.$http.post('/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error('请输入正确的用户名和密码')
        }
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
        this.$message.success('登录成功')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}
.login_container {
  display: flex;
  align-items: flex-end;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 320px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 2px 2px 10px #fff;
}
.logo {
  position: absolute;
  left: 50%;
  top: -75px;
  transform: translate(-50%);
  padding: 10px;
  width: 150px;
  height: 150px;
  background-color: #ccc;
  box-shadow: 2px 2px 10px #fff;
  border: 1px solid #ccc;
  border-radius: 50%;
  box-sizing: border-box;
}
img {
  width: 100%;
  background-color: #efefef;
  border-radius: 50%;
}
.el-form {
  width: 100%;
  padding: 0 20px;
}
.btns {
  text-align: right;
}
</style>
